package com.ext.aic.utils.http;

import android.text.TextUtils;

import com.ext.aic.constants.ApiConstants;
import com.ext.aic.utils.UserUtils;
import com.eyinfo.android_pure_utils.utils.ObjectJudge;
import com.hjq.http.config.IRequestInterceptor;
import com.hjq.http.model.HttpHeaders;
import com.hjq.http.model.HttpParams;
import com.hjq.http.request.HttpRequest;

import java.util.List;
import java.util.Set;

import okhttp3.HttpUrl;
import okhttp3.Request;

public class DynamicHostInterceptor implements IRequestInterceptor {

    @Override
    public void interceptArguments(HttpRequest<?> httpRequest, HttpParams params, HttpHeaders headers) {

    }

    private int getRemoveTagPathIndex(HttpUrl originalUrl, String pathTag) {
        List<String> segments = originalUrl.pathSegments();
        int size = segments.size();
        for (int i = 0; i < size; i++) {
            String element = segments.get(i);
            if (TextUtils.equals(element, pathTag)) {
                return i;
            }
        }
        return -1;
    }

    private Request switchRequest(Request originalRequest, HttpUrl originalUrl, HttpUrl newBaseUrl, String pathTag) {
        HttpUrl.Builder url = originalUrl.newBuilder()
                .scheme(newBaseUrl.scheme())
                .host(newBaseUrl.host())
                .port(newBaseUrl.port());
        int index = getRemoveTagPathIndex(originalUrl, pathTag);
        if (index >= 0) {
            url.removePathSegment(index);
        }
        HttpUrl newUrl = url.build();
        Request.Builder builder = originalRequest.newBuilder()
                .url(newUrl);
        addHeaders(builder);
        return builder.build();
    }

    private Request purificationRequest(Request originalRequest, HttpUrl originalUrl) {
        HttpUrl httpUrl = originalRequest.url();
        HttpUrl.Builder urlBuilder = originalUrl.newBuilder();
        Set<String> parameterNames = httpUrl.queryParameterNames();
        if (!ObjectJudge.isNullOrEmpty(parameterNames)) {
            for (String name : parameterNames) {
                String value = httpUrl.queryParameter(name);
                if (TextUtils.equals(name, "relativePath") || TextUtils.isEmpty(value) || TextUtils.equals(value, "null")) {
                    urlBuilder.removeAllQueryParameters(name);
                }
            }
        }
        Request.Builder builder = originalRequest.newBuilder();
        builder.url(urlBuilder.build());
        addHeaders(builder);
        return builder.build();
    }

    @Override
    public Request interceptRequest(HttpRequest<?> httpRequest, Request originalRequest) {
        HttpUrl originalUrl = originalRequest.url();

        String url = originalUrl.url().toString();
        if (url.contains(ApiConstants.aiPathTag)) {
            HttpUrl newBaseUrl = HttpUrl.parse(ApiConstants.baseUrlForAI);
            return switchRequest(originalRequest, originalUrl, newBaseUrl, ApiConstants.aiPathTag);
        } else if (url.contains(ApiConstants.gettingCustomersPathTag)) {
            HttpUrl newBaseUrl = HttpUrl.parse(ApiConstants.baseUrlForGettingCustomers);
            return switchRequest(originalRequest, originalUrl, newBaseUrl, ApiConstants.gettingCustomersPathTag);
        }
        return purificationRequest(originalRequest, originalUrl);
    }

    private void addHeaders(Request.Builder builder) {
        builder.addHeader("Authorization", "Bearer " + UserUtils.getInstance().getAccessToken());
    }
}
